package seqlist.singleList;

class TestNoDummyHeadLinkedList{
    private static SingalListNoDummyHead noDummyHead;
    public static void main(String[] args) {
        noDummyHead = new SingalListNoDummyHead();
        noDummyHead.addNodeByHead(new Node(1));
        noDummyHead.addNodeByHead(new Node(2));
        noDummyHead.addNodeInLast(new Node(5));
        noDummyHead.addNodeByHead(new Node(3));
        noDummyHead.addNodeByHead(new Node(4));
        noDummyHead.addByIndex(new Node(6),2);
        int i = noDummyHead.searchByVal(4);
        Node node = noDummyHead.searchByIndex(3);
        int update = noDummyHead.update(2, 10);

        System.out.println("*************插入************");
        System.out.println(noDummyHead.toString());

        System.out.println("***************查询**************");
        System.out.println(4+"的下表为"+i);
        System.out.println(node.val+"的下表为"+3);
        System.out.println("修改后原先3的位置的值由"+node.val+"修改成了"+update);
        System.out.println("*********删除头节点以后的链表********");
        noDummyHead.delNodeHead();
        System.out.println(noDummyHead.toString());
        System.out.println("*********删除尾节点以后的链表********");
        noDummyHead.delNodeByLast();
        System.out.println(noDummyHead.toString());
        System.out.println("*********删除指定值节点以后的链表********");
        noDummyHead.delByVal(10);
        System.out.println(noDummyHead.toString());



    }
}


public class SingalListNoDummyHead {
    private Node head;
    private int size = 0;
    //头插添加
    public void addNodeByHead(Node node){
        if (head == null){
            head = node;
            size++;
            return;
        }
        node.next = head;
        head = node;
        size++;
    }
    //尾插
    public void addNodeInLast(Node node){
        if (head == null){
            head = node;
            size++;
            return;
        }
        Node cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        size++;
        cur.next = node;
    }
    //指定位置插
    public void addByIndex(Node node,int index){
        if (!isTrue(index)){
            return;
        }
        if (head == null){
            head = node;
            size ++;
            return;
        }
        Node cur = head;
        for (int i = 1; i < index; i++) {
            cur = cur.next;
        }
        Node temp = cur.next;
        cur.next = node;
        node.next = temp;
    }
    //查询按下表
    public Node searchByIndex(int index){
        Node cur = head;
        if (isTrue(index)){
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            return cur;
        }
        return null;
    }
    //查询按值
    public int searchByVal(int val){
        Node cur = head;
        int index = 0;
        while (cur != null){
            if (cur.val == val){
                return index;
            }
            cur = cur.next;
            index++;
        }
        return -1;
    }
    //修改
    public int update(int index ,int val){
        Node cur = head;
        if (!isTrue(index)){
            return -1;
        }
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        int nodeVal = cur.val;
        cur.val = val;
        return nodeVal;
    }
    //删除尾节点
    public void delNodeByLast(){
        Node cur = head;
        for (int i = 0; i < size -1; i++) {
            cur = cur.next;
        }
        cur.next = null;
    }
    //删除头节点
    public void delNodeHead(){
        head = head.next;
        size--;
    }
    //删除val值节点
    public void delByVal(int val){
        Node cur = head;
        int index = searchByVal(val);
        for (int i = 0; i < index-1; i++) {
            cur = cur.next;
        }
        if (cur.next != null){
            cur.next = cur.next.next;
            size--;
        }else {
            delNodeByLast();
        }
    }
    //判断下表合法
    public boolean isTrue(int index){
        if (index < 0 || index > size){
            System.err.println("index 不合法");
            return false;
        }
        return true;
    }
    //
    public String toString(){
        Node cur = head;
        String ret = "";
        while (cur != null){
            ret += cur.val;
            ret += "->";
            cur = cur.next;
        }
        ret += null;
        return ret;
    }

}

